﻿using PickingGuide.Business;
using PickingGuide.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PickingGuideForm
{
    public partial class FrmRackSet : Form
    {
        public Action RefreshUiEvent;
        RackSetupBLL rackSetupBLL = new RackSetupBLL();
        PlcAddresBLL plcAddresBLL = new PlcAddresBLL();
        RackSetupModel _rackSetupModel;
        public FrmRackSet()
        {
            InitializeComponent();
            var list = plcAddresBLL.GetModelAll();
            cbPlcAdress.DataSource = list;
            cbPlcAdress.DisplayMember = "Address";
            cbPlcAdress.ValueMember = "ID";
            cbPlcAdress.SelectedIndex = 0;
            cbIoCardOutPut.SelectedIndex = 0;
            cbIoCardInPut.SelectedIndex = 0;
            cbRackType.SelectedIndex = 0;

        }

        public FrmRackSet(RackSetupModel model)
        {
            InitializeComponent();
            _rackSetupModel = model;

            var list = plcAddresBLL.GetModelAll();
            var addres = plcAddresBLL.GetModel(_rackSetupModel.PlcAddress);

            cbPlcAdress.DataSource = list;
            cbPlcAdress.DisplayMember = "Address";
            cbPlcAdress.ValueMember = "ID";
            cbPlcAdress.SelectedValue = addres.ID;
            cbPlcAdress.Refresh();

            cbIoCardOutPut.SelectedIndex = _rackSetupModel.IoCarOutNum -1;
            cbIoCardInPut.SelectedIndex = _rackSetupModel.IoCarInNum-1;
            cbRackType.SelectedIndex = int.Parse(_rackSetupModel.RackType)-1;

            txtRackName.Text = _rackSetupModel.Name;
            txtCode.Text = _rackSetupModel.Code;
            txtAioiAdress.Text = _rackSetupModel.AioiAddress;



            btnSave.Text = "修改";
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (_rackSetupModel == null)
            {
                var model = rackSetupBLL.GetModel(txtRackName.Text);
                if (model != null)
                {
                    MessageBox.Show("已存在相同的货架配置，不能重复！");
                    return;
                }

                var plcAdress = cbPlcAdress.SelectedItem as PlcAddresModel;

                _rackSetupModel = new RackSetupModel();
                _rackSetupModel.Name = txtRackName.Text;
                _rackSetupModel.Code = txtCode.Text;
                _rackSetupModel.PlcAddress = plcAdress.ID;
                _rackSetupModel.AioiAddress = txtAioiAdress.Text;
                _rackSetupModel.IoCarOutNum = cbIoCardOutPut.SelectedIndex + 1;
                _rackSetupModel.IoCarInNum = cbIoCardInPut.SelectedIndex + 1;
                _rackSetupModel.RackType = (cbRackType.SelectedIndex + 1).ToString();

                var result = rackSetupBLL.Add(_rackSetupModel);
                if (result.IsSuccess)
                {
                    MessageBox.Show("添加货架成功");
                    RefreshUiEvent?.BeginInvoke(null, null);
                    this.Close();
                }
                else
                    MessageBox.Show("添加货架失败：" + result.Dsca);
            }
            else
            {
                var plcAdress = cbPlcAdress.SelectedItem as PlcAddresModel;

                _rackSetupModel.Name = txtRackName.Text;
                _rackSetupModel.Code = txtCode.Text;
                _rackSetupModel.PlcAddress = plcAdress.ID;
                _rackSetupModel.AioiAddress = txtAioiAdress.Text;
                _rackSetupModel.IoCarOutNum = cbIoCardOutPut.SelectedIndex+1;
                _rackSetupModel.IoCarInNum = cbIoCardInPut.SelectedIndex + 1;
                _rackSetupModel.RackType = (cbRackType.SelectedIndex + 1).ToString();

                var result = rackSetupBLL.Update(_rackSetupModel);
                if (result.IsSuccess)
                {
                    MessageBox.Show("修改货架成功");
                    RefreshUiEvent?.BeginInvoke(null, null);
                    this.Close();
                }
                else
                    MessageBox.Show("修改货架失败：" + result.Dsca);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmRackSet_Load(object sender, EventArgs e)
        {

        }
    }
}
